package com.zhl.utils;

import com.alibaba.fastjson.JSONObject;
import io.minio.MinioClient;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by Administrator on 2018/10/19.
 * Minio文件服务器工具类
 */
public class MinioUtil {


    /**
     * 删除功能,objectName为存储桶之下的内容
     */
    public static void imageDelete(String objectName,String AccessKey,String SecretKey) {

        try {
            MinioClient minioClient = new MinioClient("http://119.29.130.216:9000", AccessKey, SecretKey);
            minioClient.removeObject("itzhl", objectName);
            System.out.println("successfully removed mybucket/myobject");
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }

    }


    /**
     *
     * @Title: uploadImage
     * @Description:上传图片
     * @param inputStream
     * @param suffix
     * @return
     * @throws Exception
     */
    public static JSONObject uploadImage(InputStream inputStream, String suffix,String AccessKey,String SecretKey) throws Exception {
        return upload(inputStream, suffix, "image/jpeg",AccessKey,SecretKey);
    }


    /**
     * @Title: uploadVideo
     * @Description:上传视频
     * @param inputStream
     * @param suffix
     * @return
     * @throws Exception
     */
    public static JSONObject uploadVideo(InputStream inputStream, String suffix,String AccessKey,String SecretKey) throws Exception {
        return upload(inputStream, suffix, "video/mp4",AccessKey,SecretKey);
    }


    /**
     * @Title: uploadVideo
     * @Description:上传文件
     * @param inputStream
     * @param suffix
     * @return
     * @throws Exception
     */
    public static JSONObject uploadFile(InputStream inputStream, String suffix,String AccessKey,String SecretKey) throws Exception {
        return upload(inputStream, suffix, "application/octet-stream",AccessKey,SecretKey);
    }


    /**
     * 上传字符串大文本内容
     *
     * @Title: uploadString
     * @Description:描述方法
     * @param str
     * @return
     * @throws Exception
     */
    /*public static JSONObject uploadString(String str) throws Exception {
        if (!StringUtils.notNullAndEmpty(str)) {
            return new JSONObject();
        }
        InputStream inputStream = new ByteArrayInputStream(str.getBytes());
        return upload(inputStream, null, "text/html");
    }*/





    /**
     * @Title: upload
     * @Description:上传主功能
     * @return
     * @throws Exception
     */
    private static JSONObject upload(InputStream inputStream, String suffix, String contentType,String AccessKey,String SecretKey) throws Exception {


        JSONObject map = new JSONObject();
        MinioClient minioClient = new MinioClient("http://119.29.130.216:9000", AccessKey, SecretKey);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String ymd = sdf.format(new Date());
        String objectName = ymd + "/" + UUID.randomUUID().toString() + (suffix != null ? suffix : "");

        try {
            minioClient.putObject("itzhl", objectName, inputStream, contentType);
            String url = minioClient.getObjectUrl("itzhl", objectName);
            map.put("flag", "0");
            map.put("mess", "上传成功");
            map.put("url", url);
            map.put("urlval", url);
            map.put("objectName", objectName);
            map.put("path", "lignziyang" + "/" + objectName);

        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", "-1");
            map.put("mess", "上传失败");
            map.put("url", null);
            map.put("urlval", null);
            map.put("objectName", objectName);
            map.put("path", null);
        }
        return map;
    }

}
